<template>
  <div class="score_container" ref="scoreRef" :style="{ width, height }"></div>
</template>
<script>
import * as echarts from "echarts";

export default {
  props: {
    id: {
      type: String,
      default: ""
    },
    width: {
      type: String,
      default: "400px"
    },
    height: {
      type: String,
      default: "400px"
    },
    score: {
      type: Number,
      default: 0
    }
  },

  watch: {
    score: {
      handler() {
        this.$nextTick(function () {
          this.init();
        });
      }
    }
  },

  methods: {
    init() {
      const { score } = this.$props;
      const option = {
        backgroundColor: "transparent",
        // backgroundColor: "#1D4175",
        title: [
          {
            text: "课堂得分",
            x: "center",
            top: "55%",
            textStyle: {
              color: "#C2C2C2",
              fontSize: 12,
              fontWeight: "100"
            }
          },
          {
            text: score,
            x: "center",
            top: "35%",
            textStyle: {
              fontSize: "20",
              color: "#F5FCFF",
              foontWeight: "500"
            }
          }
        ],
        // 极坐标系
        polar: {
          radius: [65, "55%"]
        },
        // 极坐标系：角度轴
        angleAxis: {
          max: (100 * 360) / 270,
          show: false,
          type: "value",
          startAngle: 225
        },
        // 极坐标系：径向轴
        radiusAxis: {
          type: "category",
          show: true,
          axisLabel: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          }
        },
        series: [
          // 第二层：数据以进度条的形式展示
          {
            type: "bar",
            data: [
              {
                value: score
              }
            ],
            itemStyle: {
              color: function () {
                let obj = {
                  type: "linear",
                  x: 0, //右
                  y: 0.5, //下
                  x2: 1, //左
                  y2: 0, //上
                  colorStops: [
                    {
                      offset: 0,
                      color: "#8ac4d4"
                    },
                    {
                      offset: 1,
                      color: "#07B2D6"
                    }
                  ]
                };

                if (score >= 0 && score < 20) {
                  obj.colorStops[1].color = "#8ac4d4";
                } else if (score >= 20 && score < 40) {
                  obj.y = 0.3;
                  obj.x2 = 2;
                } else if (score >= 40 && score < 60) {
                  obj.y = 0.3;
                  obj.x2 = 1.5;
                } else if (score >= 60 && score < 80) {
                  obj.y = 0.4;
                  obj.x2 = 1.1;
                }
                return obj;
              }
            },
            barGap: "-100%",
            coordinateSystem: "polar",
            roundCap: true,
            cursor: "auto",
            z: 2
          },
          // 第二层：进度条背景
          {
            type: "bar",
            data: [
              {
                value: 100
              }
            ],
            itemStyle: {
              color: "#013f72"
            },
            barGap: "-100%",
            coordinateSystem: "polar",
            roundCap: true,
            cursor: "auto",
            emphasis: {
              itemStyle: {
                color: "#013f72"
              }
            },
            z: 1
          },
          // 第一层：中心文字展示区
          {
            type: "pie",
            radius: ["0%", "50%"],
            hoverAnimation: false,
            center: ["50%", "50%"],
            cursor: "auto",
            itemStyle: {
              normal: {
                labelLine: {
                  show: false
                },
                color: {
                  type: "radial",
                  x: 0.5,
                  y: 0.5,
                  r: 0.5,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#174869"
                    },
                    {
                      offset: 1,
                      color: "#08385c"
                    }
                  ]
                }
              }
            },
            data: [
              {
                value: 100
              }
            ],
            z: 2
          },
          // 第三层；仪表盘：只显示刻度
          {
            type: "gauge",
            radius: "60%",
            startAngle: "225",
            endAngle: "-45",
            center: ["50%", "50%"],
            min: 0,
            max: 100,
            pointer: {
              show: false
            },
            title: {
              show: false
            },
            detail: {
              show: false
            },
            data: [
              {
                value: 100,
                name: ""
              }
            ],
            axisLine: {
              lineStyle: {
                width: 20,
                color: [
                  [0, "#5fa7ca"],
                  [1, "#5fa7ca"]
                ],
                opacity: 0
              }
            },
            axisTick: {
              show: false,
              length: 14,
              lineStyle: {
                color: "auto"
              }
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              show: true,
              distance: 18, //距离刻度的距离
              lineHeight: -50,
              formatter: function (value) {
                return value % 100 === 0 ? value : "";
              }
            }
          },
          // 第四层；背景圆：带阴影
          {
            type: "pie",
            radius: ["0%", "80%"],
            hoverAnimation: false,
            center: ["50%", "50%"],
            cursor: "auto",
            itemStyle: {
              color: {
                type: "radial",
                x: 0.5,
                y: 0.5,
                r: 0.5,
                colorStops: [
                  {
                    offset: 0,
                    color: "#002e50"
                  },
                  {
                    offset: 0.9,
                    color: "#002e50"
                  },
                  {
                    offset: 1,
                    color: "#134568"
                  }
                ]
              }
            },
            data: [
              {
                value: 100
              }
            ],
            labelLine: {
              show: false
            },
            z: -1
          },
          // 第五层：视觉上类似于边框，带阴影
          {
            type: "pie",
            radius: ["0", "80.5%"],
            hoverAnimation: false,
            center: ["50%", "50%"],
            cursor: "auto",
            itemStyle: {
              color: "#146a90",
              shadowBlur: 50,
              shadowColor: "#146a90"
            },
            data: [
              {
                value: 100
              }
            ],
            labelLine: {
              show: false
            },
            emphasis: {
              itemStyle: {
                color: "#146a90"
              }
            },
            z: -2
          }
        ]
      };

      const dom = this.$refs.scoreRef;
      const scoreChart = echarts.init(dom);
      scoreChart.setOption(option, true);

      window.addEventListener("resize", function () {
        scoreChart.resize();
      });
      scoreChart.resize();
    }
  },
  mounted() {
    this.$nextTick(function () {
      this.init();
    });
  }
};
</script>
<style lang="scss" scoped>
.score_container {
  /deep/ canvas {
    position: absolute;
    top: -30px !important;
  }
}
</style>
